using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using XLua;

public class FirstLua : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        FirstLuaTest();
        Debug.Log("+++++++++++++++++++");
        DoString();
        //默认加载器StreamingAssets
        Debug.Log("+++++++++++++++++++");
        MyLoder();
    }

    private void MyLoder()
    {
        LuaEnv luaEnv = new LuaEnv();

        //luaEnv.DoString("require('text')");
        luaEnv.AddLoader(ProjectLoader);

        luaEnv.DoString("require('text')");

        luaEnv.Dispose();
    }
    /// <summary>
    /// 自定义加载器
    /// </summary>
    /// <param name="filepath"></param>
    /// <returns></returns>
    private byte[] ProjectLoader(ref string filepath)
    {
        Debug.Log("自定义加载器:" + filepath);
        //lua脚本的路径是自己定制的（可以放入ab包中）
        string path = Application.dataPath;

        path = path.Replace("Assets", "LuaScripts" + "/" + filepath + ".lua");

        if (File.Exists(path))
        {
            return File.ReadAllBytes(path);
        }
        else
        {
            return null;
        }
    }

    private void DoString()
    {
        LuaEnv luaEnv = new LuaEnv();
        //输出
        luaEnv.DoString("CS.UnityEngine.Debug.Log('lua转Cs')");
        //调用函数
        luaEnv.Dispose();
    }

    private void FirstLuaTest()
    {
        LuaEnv luaEnv = new LuaEnv();

        luaEnv.DoString("require('text')");

        luaEnv.Dispose();
    }
}
